﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;
using System.Configuration;

namespace Sami_SMS_CRM_UI
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        #region Properties
        public User CurUser
        {
            get
            {
                if (Session[_SESSION.USER_NAME] == null) return null;
                return (User)Session[_SESSION.USER_NAME];
            }
            set
            {
                if (Session[_SESSION.USER_NAME] == null)
                    Session.Add(_SESSION.USER_NAME, value);
                else Session[_SESSION.USER_NAME] = value;
            }
        }
        public string AccessDeniedURL
        {
            get { return ConfigurationManager.AppSettings["AccessDeniedURL"]; }
        }
        public string DefaultURL
        {
            get { return ConfigurationManager.AppSettings["DefaultURL"]; }
        }
        public string LoginURL
        {
            get { return ConfigurationManager.AppSettings["LoginURL"]; }
        }
        public string ReturnURL
        {
            get
            {
                string returnURL = (string)Session[_SESSION.RETURN_URL];
                if (returnURL == null || returnURL == AccessDeniedURL || returnURL == LoginURL) return DefaultURL;
                return returnURL;
            }
            set
            {
                if (Session[_SESSION.RETURN_URL] == null)
                    Session.Add(_SESSION.RETURN_URL, value);
                else
                    Session[_SESSION.RETURN_URL] = value;
            }
        }
        public string LogoutURL
        {
            get { return ConfigurationManager.AppSettings["LogoutURL"]; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurUser == null)
                {
                    ltrUserInfo.Visible = false;
                    lbtLog.Text = "Login";
                }
                else
                {
                    ltrUserInfo.Visible = true;
                    ltrUserInfo.Text = (Session[_SESSION.USER_NAME] as Sami_SMS_CRM_Business.User).UserName;
                    lbtLog.Text = "Logout";
                }
            }
        }

        protected void lbtLog_Click(object sender, EventArgs e)
        {
            if (CurUser == null)
            {
                Response.Redirect(LoginURL);
            }
            else
            {
                CurUser = null;
                Response.Redirect(LogoutURL + Request.Url.AbsoluteUri);
            }
        }
    }
}
